/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gemom.combinationItems;

import com.gemom.*;
import com.gemom.util.*;

import org.apache.commons.math.distribution.NormalDistributionImpl;
import org.apache.commons.math.MathException;

import java.util.*;
/**
 *
 * @author Yair D
 */
public class BidG {


    Vector<Bid> listBids;

    public BidG(Item[] arrayItems,CovMatrixData covM, GBroker broker)
    {

        Random genItem = new Random();
        listBids = new Vector<Bid>();


    for(int i=0;i<5;i++)//number of bids
    {
        double sum=0;
        Bid currentBid=new Bid(i);////pilas la ID
        currentBid.setBrokerIdFlag(broker.getBrokerID());

        //adding items with no repetitions and <= brokerRate
        while(sum<=broker.getBrokerRate())
        {
            int current = genItem.nextInt( arrayItems.length );
            if(!currentBid.getItemsVec().contains(arrayItems[current]))
            {
                sum+=arrayItems[current].getItemRate();
                if(sum<=broker.getBrokerRate())
                    currentBid.addItem(arrayItems[current]);
             }
        }//end while-loop

        //checking for repeated bids
        int suma=0;
        for(int k=0;k<listBids.size();k++){
                suma=0;
                //down here it checks first the number of items in current bid against other bids
                if(currentBid.getItemsVec().size()==listBids.get(k).getItemsVec().size())
                        for(int l=0;l<currentBid.getItemsVec().size();l++){
                                if(currentBid.getItemsVec().get(l).getItemID()== listBids.get(k).getItemsVec().get(l).getItemID())
                                        {suma+=1;}
                        }//end-for
                if(suma==currentBid.getItemsVec().size())break;



        }//end-for checking repeated bids

        if(suma==currentBid.getItemsVec().size()){--i;}
        else//set price and add it to bidlist
        {


            double pro=0;
            int[]itemIDs= new int[currentBid.getItemsVec().size()];//this is for calcVARCOVs
            double RS=0;
            double mean=0;
            for(int j=0;j<currentBid.getItemsVec().size();j++)
            {
                RS+=currentBid.getItemsVec().get(j).getRevenueItem();
                mean+=currentBid.getItemsVec().get(j).getItemRate();
                itemIDs[j]=currentBid.getItemsVec().get(j).getItemID();
            }

            double varian =covM.calculateVarsetItems(itemIDs);
            double sDev= Math.sqrt(varian);
            NormalDistributionImpl normalprob = new NormalDistributionImpl(mean,sDev);
            try{pro=normalprob.cumulativeProbability(broker.getBrokerRate());}///k here? each broker
            catch(MathException e){System.out.println("pilas");pro=1;}
            double price=RS +(3*RS*pro);

            currentBid.price=price;
            listBids.add(currentBid);


        }//end-else

    }//end outer-loop

 }//end constructor

   public  Vector<Bid> getlistBids(){return listBids;}

}//end class
